1 module std.stdio;
2 import core.stdc.stdio;
3 
4 version(WebAssembly)
5 {
6     import arsd.webassembly;
7     void writeln(T...)(T t) {
8         eval(q{
9             console.log.apply(null, arguments);
10         }, t);
11     }
12 }
13 version(PSVita)
14 {
15     extern(C) void hipVitaPrint(uint length, const(char)* str);
16     void writeln(Args...)(Args args)
17     {
18         import hip.util.conv:to;
19         char[] str;
20         static foreach(arg; args){str~= to!string(arg);}
21         hipVitaPrint(str.length, cast(const(char)*)str.ptr);
22         import hip.util.memory;
23         freeGCMemory(str);
24     }
25 }
26 version(CustomRuntimeTest)
27 {
28     void writeln(Args...)(Args args)
29     {
30         import hip.util.conv:to;
31         char[] str;
32         static foreach(arg; args){str~= to!string(arg);}
33         printf("%.*s", str.length, cast(const(char)*)str.ptr);
34         import hip.util.memory;
35         freeGCMemory(str);
36     }
37 }
38 
39 struct File
40 {
41     FILE* fptr;
42     private size_t _size;
43 
44     size_t size(){return _size;}
45 
46     this(string path, string openMode = "r")
47     {
48         fptr = fopen((path~'\0').ptr, (openMode~'\0').ptr);
49         if(fptr != null)
50         {
51             fseek(fptr, 0, SEEK_END);
52             auto tempSize = ftell(fptr);
53             if(tempSize > 0)
54                 _size = cast(size_t)tempSize;
55             fseek(fptr, 0, SEEK_SET);
56         }
57     }
58 
59     void rawWrite(string data){rawWrite(cast(ubyte[])data);}
60     void rawWrite(ubyte[] data)
61     {
62         if(fptr != null)
63         {
64             foreach(b; data)
65             {
66                 if(fputc(b, fptr) == EOF)
67                     break;
68             }
69         }
70     }
71 
72     void[] rawRead(void[] buffer){return cast(void[])rawRead(cast(ubyte[])buffer);}
73     ubyte[] rawRead(ubyte[] buffer)
74     {
75         if(fptr != null)
76         {
77             int ch = -1;
78             size_t totalRead = 0;
79             while((ch = fgetc(fptr)) != EOF && totalRead < buffer.length)
80                 buffer[totalRead++] = cast(ubyte)ch;
81             
82             return buffer[0..totalRead];
83         }
84         return buffer;
85     }
86 
87     void seek(ptrdiff_t offset, int origin = SEEK_SET)
88     {
89         if(fptr != null)
90         {
91             fseek(fptr, offset, origin);
92         }
93     }
94     void close()
95     {
96         if(fptr != null)
97         {
98             fclose(fptr);
99             fptr = null;
100         }
101     }
102 
103     ~this()
104     {
105         if(fptr != null)
106         {
107             fclose(fptr);
108             fptr = null;
109         }
110     }
111 }